
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class CubicSolver {
    
    public static ArrayList<Line> getLines(String filename) throws IOException{
        ArrayList<Line> segments = new ArrayList<Line>();
        FileInputStream fis = new FileInputStream(filename);
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line = br.readLine()) != null) {
            String[] points = line.split(" ", 4);
            double x1 = Double.parseDouble(points[0]);
            double y1 = Double.parseDouble(points[1]);
            double x2 = Double.parseDouble(points[2]);
            double y2 = Double.parseDouble(points[3]);
            Point p1 = new Point(x1, y1);
            Point p2 = new Point(x2, y2);
            Line segment;
            if(p1.getY() > p2.getY() || (p1.getY() == p2.getY() && p1.getX() <= p2.getX())){
                segment = new Line(p1, p2);
                segments.add(segment);
            }
            else if(p1.getY() < p2.getY() || (p1.getY() == p2.getY() && p1.getX() > p2.getX())){
                segment = new Line(p2, p1);
                segments.add(segment);
            }        
        }
        return segments;
    }
    
    public static double[] getCoefs(Line line){
        double x1 = line.p1.getX();
        double x2 = line.p2.getX();
        double y1 = line.p1.getY();
        double y2 = line.p2.getY();
        double m = 0;
        if((x1-x2) != 0)return new double[]{(y1-y2) / (x1-x2), y1 - y2 * m};
        else return new double[]{x1};
    }
    
    public static ArrayList<Point> solve(ArrayList<Line> segments){
        ArrayList<Point> result = new ArrayList<Point>();
        for(int i = 0; i < segments.size(); i++){
            Line l1 = segments.get(i);
            for(int j = i + 1; j < segments.size(); j++){
                Line l2 = segments.get(j);
                double[] coefs1 = getCoefs(l1);
                double[] coefs2 = getCoefs(l2);
                if(coefs1.length == 2 && coefs2.length ==2){
                    System.out.println("2-2");
                    double x = (coefs1[0] - coefs2[0]) / (coefs2[1] - coefs1[1]);
                    double y = coefs1[0] + coefs1[1] * x;
                    Point p = new Point(x,y);
                    if(l1.contains(p)) result.add(p);
                }
                else if(coefs1.length == 2 && coefs2.length == 1){
                    double x = coefs2[0];
                    double y = coefs1[0] + coefs1[1] * x;
                    Point p = new Point(x,y);
                    if(l1.contains(p)) result.add(p);
                }
                else if(coefs1.length == 1 && coefs2.length == 2){
                    double x = coefs1[0];
                    double y = coefs2[0] + coefs2[1] * x;
                    Point p = new Point(x,y);
                    if(l1.contains(p)) result.add(p);
                }
                else if(coefs1.length == 1 && coefs2.length == 1){
                    if(coefs1[0] == coefs2[0]){
                        if(l1.p1 == l2.p1 || l1.p1 == l2.p2){
                            Point p = new Point(coefs1[0], l1.p1.getY());
                            result.add(p);
                        }
                        else if(l1.p2 == l2.p1 || l1.p2 == l2.p2){
                            Point p = new Point(coefs1[0], l1.p2.getY());
                            result.add(p);
                        }
                    }
                }
            }
        }
        return result;
    }
    
    public static void main(String[] args) throws IOException{
        ArrayList<Line> segments = getLines("./files/fichier1.txt");
        ArrayList<Point> result = solve(segments);
        for(Point p: result) System.out.println(p);
        System.out.println("Taille = " + result.size());
    }
}
